package com.erp.module.duizhangPhoto

import android.app.Activity
import android.content.Intent
import android.view.View
import android.widget.AdapterView
import androidx.appcompat.app.AlertDialog
import com.erp.R
import com.erp.bean.DuizhangBean
import com.erp.module.duizhangPhoto.XxDuizhangPhoto.XxDuizhangPhotoActivity
import com.erp.myAdapter.DuizhangAdapter
import com.erp.myFragment.BaseKFragment
import com.erp.util.Myutil
import com.erp.view.PullToRefreshView
import kotlinx.android.synthetic.main.fragmengt_duizhangphoto.*
import java.util.ArrayList

class FragmentDuizhangPhoto : BaseKFragment(), View.OnClickListener,DuizhangPhotoContract.DuizhangPhotoPView, AdapterView.OnItemClickListener, PullToRefreshView.OnFooterRefreshListener, PullToRefreshView.OnHeaderRefreshListener {

    var updatedialog: AlertDialog? = null
    lateinit var P: IDuizhangPhotoP
    lateinit var adapter: DuizhangAdapter
    lateinit var duizhanglist: MutableList<DuizhangBean>
    override fun getLayoutId(): Int {
        return R.layout.fragmengt_duizhangphoto
    }

    override fun initData() {

        duizhanglist = ArrayList()
        LlXingJian.setOnClickListener(this)
        adapter = DuizhangAdapter(duizhanglist, activity!!)
        P = IDuizhangPhotoP( duizhanglist, this, activity!!)
        lvFragmentDuizhangPhoto.adapter = adapter
        lvFragmentDuizhangPhoto.onItemClickListener = this
        Pull.setheader(true)
        Pull.setOnFooterRefreshListener(this)
        Pull.setOnHeaderRefreshListener(this)
        Pull.headerRefreshing()
    }

    override fun onClick(v: View?) {
        when(v){
            LlXingJian -> {
                val intent = Intent(activity, XxDuizhangPhotoActivity::class.java)
                intent.putExtra("code", 2)
                startActivityForResult(intent, 3)
            }
        }
    }

    override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
        val intent = Intent(activity, XxDuizhangPhotoActivity::class.java)
        intent.putExtra("code", 1)
        intent.putExtra("id", duizhanglist[position].id)
        startActivityForResult(intent, 3)
    }

    fun  qingkong(){
        if(isFirstLoad){
            return
        }
        Pull.headerRefreshing()
    }

    override fun xialaOk() {
        updatedialog?.dismiss()
        Pull.onHeaderRefreshComplete()
        adapter.notifyDataSetChanged()
    }

    override fun xialaNo() {
        updatedialog?.dismiss()
        Pull.onHeaderRefreshComplete()
    }

    override fun shanglaOk() {
        updatedialog?.dismiss()
        Pull.onFooterRefreshComplete()
        adapter.notifyDataSetChanged()
    }

    override fun shanglaNo() {
        updatedialog?.dismiss()
        Pull.onFooterRefreshComplete()
    }
    override fun gainDataOk() {
        adapter.notifyDataSetChanged()
    }

    override fun onFooterRefresh(view: PullToRefreshView) {
        P.shangla()
    }
    override fun onHeaderRefresh(view: PullToRefreshView) {
        P.xiala()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (data == null) {
            return
        }
        if (resultCode == Activity.RESULT_OK) {
            when (requestCode) {
                3 -> {
                    updatedialog = Myutil.showupdatedialog(activity, "正在刷新", false)
                    Pull.headerRefreshing()
                }
            }
        }
    }
}